/**
 @author 迷途小羔羊
 2014-11-19
 */


	/**
	 * 加载资源，用于记载资源时，记录资源的一些基本信息
	 * 当要销毁时，请调用其Clear方法，并且把引用设置为null
	 * */
	class LoadInfo
	{
		public static SEND_RES:number = 1;
		public static SEND_DATA:number = 2;
		public static SEND_HTTP:number = 3;
		public save:Boolean=true;
		public path:string;
		public bytes:ByteArray;
		public startTime:number;
		private _content;
		public contentType:string;
		public header:Array<any>;
		public type:any;
		public method:string;
		public thisObject:any;
		public param:any;
		public data:any;
		/**传送类型，参考LoadInfo常量*/public sendType:number;
		private _msg:string;
		private _progFunc:Function;
		private _callBackFunc:Function;
		private _loadVec:Array<LoadInfo>;
		public mimeType:string;
		private static _pool;
		// private static _s:Single;
		public constructor()//s:Single
		{
			// if(!s)
			// 	throw(new Error("use getInstance to Create"));
		}
		public get dataFormat():string
		{var s = this;
			if(s.type == GYLoader.TYPE_IMAGE)
				return "image";
			else if(s.type == GYLoader.TYPE_BINARY)
				return "arraybuffer";
			else if(s.type == GYLoader.TYPE_TEXT)
				return "text";
			else if(s.type == GYLoader.TYPE_JSON)
				return "json";
			return "";
		}
		public set callBackFunc(val:Function)
		{
			this._callBackFunc=val;
		}
		/**加载完成回调函数*/
		public get callBackFunc():Function
		{
			return this._callBackFunc;
		}
		public set msg(val:string)
		{
			var s = this;
			s._msg=val;
			var len:number;
			if(s._loadVec==null)
				return;
			len=s._loadVec.length;
			while(--len>-1)
				s._loadVec[len].msg = val;
		}
		/**错误信息*/
		public get error():number
		{
			var s = this;
			var ind:number = s._msg.indexOf("#");
			if(ind == -1)
				return 0;
			return Number(s._msg.substr(ind+1));
		}
		/**加载信息*/
		public get msg():string
		{
			return this._msg;	
		}
		public set content(val:ResObject)
		{
			var s = this;
			if(!s.save && s._content)
				s._content.clear(false);
			s._content=val;
			if(s._loadVec==null)
				return;
			var len:number;
			len=s._loadVec.length;
			while(--len>-1)
				s._loadVec[len]._content = val;
		}
		public get content():ResObject
		{
			return this._content;	
		}
		/**加载完成，会调用所有关联的回调*/
		public callBack():void
		{
			var s = this;
			if(s._callBackFunc!=null)
				s._callBackFunc.call(s.thisObject,s);
			if(s._loadVec==null)
				return;
			var len:number;
			let loadInfo:LoadInfo;	
			len=s._loadVec.length;
			while(--len>-1)
			{
				loadInfo = s._loadVec[len];
				loadInfo._msg = s._msg;
				if(loadInfo.callBackFunc!=null)
					loadInfo.callBackFunc.call(loadInfo.thisObject,loadInfo);
			}
		}
		/**取消加载请求,当加载请求全部被取消时，自动清理这个LoadInfo
		 * @return 是否已无加载请求
		 * */
		public cancel(callBackFunc:Function, thisObject:any):Boolean
		{
			var s = this;
			var len:number=0;
			if(s._loadVec)len=s._loadVec.length;
			if(s._callBackFunc==callBackFunc && s.thisObject == thisObject)
			{
				if(len == 0)
				{
					s.clear();
					return true;
				}
				else
				{
					s.copy(s._loadVec.shift());
				}
				return false;
			}
			while(--len>-1)
			{
				if(s._loadVec[len].callBackFunc==callBackFunc && s.thisObject == thisObject)
					s._loadVec.splice(len,1);
			}
			return false;
		}
		public set progFunc(val:Function)
		{
			this._progFunc=val;
		}
		/**加载进度回调函数*/
		public get progFunc():Function
		{
			return this._progFunc;
		}
		public prog(l:SeqURLLoader):void
		{
			var s = this;
			if(s._progFunc!=null)
				s._progFunc.call(s.thisObject,l);
			var len:number;
			if(s._loadVec==null)
				return;
			len=s._loadVec.length;
			while(--len>-1)
			{
				if(s._loadVec[len].progFunc!=null)
					s._loadVec[len].progFunc.call(s._loadVec[len].thisObject,l);
			}
		}
		/**清理回收*/
		public clear():void
		{
			var s = this;
			LoadInfo._pool.push(this);
			s.path=null;
			s.startTime=0;
			s.progFunc=null;
			s.callBackFunc=null;
			s.contentType=null;
			s.header = null;
			
			s.type=0;
			s.method=null;
			s.msg=null;
			s.bytes = null;
			s.data = null;
			s.sendType = 0;
			if(!s.save)
			{
				s.content.clear(false);
				s.save = true;				
			}			
			s.content=null;
			s.thisObject = null;
			s.param = null;
			s.mimeType = null;
			if(s._loadVec)
			{
				let len:number = s._loadVec.length;
				while(--len>-1)
				{
					s._loadVec[len].clear();	
				}
				s._loadVec.length = 0;
			}
		}
		/**同一个url的加载请求，将被添加到第一个加载请求的LoadInfo下面*/
		public addLoadInfo(val:LoadInfo, thisObject:any):void
		{
			var s = this;
			var len:number;
			if(s._loadVec==null)
				s._loadVec=[];
			len = s._loadVec.length;
			while(--len>-1)
			{
				if(s._loadVec[len].callBackFunc == val.callBackFunc  && s._loadVec[len].thisObject == thisObject)
					return;
			}
			if(s.callBackFunc == val.callBackFunc && s.thisObject == thisObject)
				return;
			s._loadVec.push(val);
		}
		private copy(l:LoadInfo):void
		{
			var s = this;
			s.save = l.save;
			s.path = l.path;
			s.bytes = l.bytes;
			s.startTime = l.startTime;
			s._content = l.content;
			s.type = l.type;
			s.method = l.method;
			s.data = l.data;
			s.contentType = l.contentType;
			s.param = l.param;
			s.mimeType = l.mimeType;
			s.sendType = l.sendType;
			s._msg = l.msg;
			s._progFunc = l.progFunc;
			s._callBackFunc = l.callBackFunc;
			s.thisObject = l.thisObject;
		}
			
		public static getInstance():LoadInfo
		{
			if(!LoadInfo._pool)
			{
				// _s=new Single;
				LoadInfo._pool=[];
			}
			if(LoadInfo._pool.length==0)
				return new LoadInfo();
			return LoadInfo._pool.pop();
		}
	}
